<template>
  <el-dialog @open="openDialog" :title="isAdd?'新增':'编辑'" :visible="dialogFormVisible" @close="closeDialog">
    <el-form :model="form" label-width="80px" :rules="rules">
      <!--<el-form-item label="头像" prop="avatar">
        <el-upload
          class="avatar-uploader"
          :action="host+'/guard/upload?module=avatar'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="form.avatar" :src="host+form.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>-->
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="护照号" prop="passport">
        <el-input v-model="form.passport"></el-input>
      </el-form-item>


      <el-form-item label="积分" prop="age">
        <el-input type="number" v-model.number="form.point"></el-input>
      </el-form-item>
      <el-form-item label="生日" >
        <el-date-picker
          v-model="form.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
     <!-- <el-form-item label="性别" prop="gender">
        <el-radio v-model="form.gender" label="1">男</el-radio>
        <el-radio v-model="form.gender" label="2">女</el-radio>
      </el-form-item>-->
     <!-- <el-form-item label="地址" prop="address">
        <el-input v-model="form.address"></el-input>
      </el-form-item>-->
      <el-form-item label="备注" prop="beizhu">
        <el-input :rows="3" type="textarea" v-model="form.beizhu"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch
          v-model="form.status"
          :active-value="1"
          :inactive-value="2"
          active-text="启用"
          inactive-text="禁用">
        </el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

import {myPost} from "@/utils/myUtils";

const urlAdd = '/guard/user/add'

export default {
  name: "AddUserDialog",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    record: {
      type: Object,
      default: {}
    }
  },

  data() {
    let checkPhone = (rule, value, callback) => {
      const reg = /^[1][3-9][0-9]{9}$/;
      if (value == '' || value == undefined || value == null) {
        callback();
      } else {
        if ((!reg.test(value)) && value != '') {
          callback(new Error('请输入正确的电话号码'));
        } else {
          callback();
        }
      }
    }
    return {
      form: {
        id: 0,
        name: '',
        passport: '',
        phone: '',
        //age: 20,
        //gender: '1',
        beizhu: '',
        birthday:'',
        point:0,
        //avatar: '',
        //address: '',
        status: 1,
      },
      rules: {
        passport: [
          {required: true, message: '请输入', trigger: 'blur'},
        ],
        name: [
          {required: true, message: '请输入', trigger: 'blur'},
        ],
        age: [
          {required: true, message: '请输入'},
          {type: 'number', message: '积分必须为数字值'}
        ],
        phone: [
          {required: true, message: '请输入', trigger: 'blur'},
          {validator: checkPhone, trigger: 'blur'}
        ],
        /* email:[
            { required: true, message: '请输入邮箱格式', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
        ], */
      },
      host: process.env.VUE_APP_HOST
    }
  },
  methods: {

    handleAvatarSuccess(res, file) {
      if (res.status) {
        this.form.avatar = res.data.records.filePath
      } else {
        this.$message.error('上传失败!')
      }
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },

    onSubmit() {
      myPost(urlAdd, this.form).then(res => {
        if (res) {
          this.$message.success(res.msg)
          this.$emit('success', false)
        }
      })
    },
    cancel() {
      this.$emit('cancelDialog', false)
      Object.assign(this.$data.form, this.$options.data().form)
    },
    closeDialog() {
      this.cancel()
    },
    openDialog() {
      console.log(this.record)
      Object.assign(this.$data.form, this.$options.data().form)
      //record有数据说明是编辑
      if (Object.keys(this.record).length > 0) {
        this.form.id = this.record.id
        this.form.name = this.record.name
        this.form.passport = this.record.passport
        this.form.phone = this.record.phone
        this.form.birthday = this.record.birthday
        this.form.point = this.record.point
        // this.form.address = this.record.address
        this.form.beizhu = this.record.beizhu
        this.form.status = this.record.status
      }
    },
  },

  computed: {
    isAdd: function () {
      return Object.keys(this.record).length <= 0;
    }
  },

}
</script>

<style scoped>

</style>
